import json
import re
import requests
import os
import sys
from selenium import webdriver

browser = webdriver.Chrome()
browser.get("https://v.douyin.com/JMYayLY/")


def get_rootpath():
    path = os.path.realpath(sys.argv[0])
    if os.path.isdir(path):
        return path
    elif os.path.isfile(path):
        return os.path.dirname(path)


sys.exit(0)


with open(get_rootpath()+"/url.txt", "r", encoding='UTF-8') as f:
    data = f.readlines()
result = [re.match(r'<name>(.+)<\/name><url>(.+)<\/url>', i,
                   re.M | re.I).group(2).split("&ratio")[0] for i in data]

headers = {
    'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
}
filePath = get_rootpath()+"/无水印"
if not os.path.exists(filePath):
    os.mkdir(filePath)
count = 0
for res1 in result:
    count += 1
    videoBin = requests.get(res1, headers=headers)
    print(videoBin)
    with open(filePath+str(count)+'.mp4', 'wb') as fb:
        fb.write(videoBin.content)
